package cn.yunhe.controller;


import cn.yunhe.pojo.GllsResult;
import cn.yunhe.pojo.PageBean;
import cn.yunhe.pojo.RoleP;
import cn.yunhe.pojo.TreeNode;
import cn.yunhe.service.RoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleUserService roleUserService;


    @RequestMapping("/list")
    public String listDeptAndParent(Model model, PageBean pageBean) {
        PageBean pb = roleUserService.listDeptOfPage(pageBean);
        pb.setUrl("/role/list");
        model.addAttribute("pb", pb);
        System.out.println(pb.getDatas().get(1));
        return "sysadmin/role/jRoleList";
    }

    @RequestMapping("/toview")
    public String toview(String id, Model model) {
        RoleP roleP = roleUserService.findUserById(id);
        model.addAttribute("role", roleP);
        System.out.println(roleP);
        return "sysadmin/role/jRoleView";
    }

    @RequestMapping("/tocreate")
    public String tocreate() {


        return "sysadmin/role/jRoleCreate";
    }

    @RequestMapping("insert")
    public String insert(RoleP roleP) {

        int re = roleUserService.insertData(roleP);

        return "redirect:/role/list";
    }

    @RequestMapping("tomodule")
    public String tomodule(String id, Model model) {
        RoleP roleP = roleUserService.findUserById(id);
        model.addAttribute("role", roleP);


        return "sysadmin/role/jRoleModule";
    }

    @RequestMapping("/listModuleOfTreeBeanByRoleId")
    @ResponseBody
    public List<TreeNode> listModuleOfTreeBeanByRoleId(String roleId) {

        List<TreeNode> treeNodes = roleUserService.listModuleOfTreeBeanByRoleId(roleId);

        System.out.println("json格式" + treeNodes.toString());
        return treeNodes;
    }

    @RequestMapping("toupdate")
    public String toupdate(String id, Model model) {
        RoleP roleP = roleUserService.findUserById(id);
        model.addAttribute("role", roleP);

        return "sysadmin/role/jRoleUpdate";
    }

    @RequestMapping("update")
    public String update(RoleP roleP) {
        int re = roleUserService.updateRoleP(roleP);
        return "redirect:/role/list";
    }

    @RequestMapping("delete")
    @ResponseBody
    public GllsResult delete(String[] roleIds) {
        GllsResult result = roleUserService.deleteById(roleIds);
        return result;
    }


}
